﻿using System.Collections.ObjectModel;
using MyBookReadsList.Commands;
using MyBookReadsList.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Controls;

namespace MyBookReadsList.ViewModels
{
    public class BookViewModel : ViewModelBase, IPageViewModel
    {
        public Book Book { get; set; }
        private ObservableCollection<Comment> comments;
        public IEnumerable<Comment> Comments 
        {
            get
            {
                if (this.comments == null)
                {
                    this.Comments = Data.DataPersister.LoadComments(Book.Id);
                  
                }
                return this.comments;
            }
            set
            {
                if (this.comments == null)
                {
                    this.comments = new ObservableCollection<Comment>();
                }
                this.comments.Clear();
                foreach (var item in value)
                {
                    this.comments.Add(item);
                }
            }
        }

        private CommentAdd currentComment;
        public CommentAdd CurrentComment 
        {
            get
            {
                return this.currentComment;
            }

            set
            {
                this.currentComment = value;
                this.OnPropertyChanged("CurrentComment");
            }
        }

        public string Name
        {
            get
            {
                return "Page list view";
            }
        }

        private ICommand addComment;
        public ICommand AddComment
        {
            get
            {
                if (this.addComment == null)
                {
                    this.addComment = new RelayCommand(this.HandleAddCommentCommand);
                }
                return this.addComment;
            }
        }

        public void HandleAddCommentCommand(object obj)
        {
            currentComment.BookId =int.Parse(Book.Id);
            Data.DataPersister.AddComment(currentComment);
        }

        public BookViewModel()
        {
            this.CurrentComment = new CommentAdd();
        }

    }
}
